package com.xycake.controller.client;

import com.xycake.entity.Order;
import com.xycake.entity.User;
import com.xycake.service.GoodsService;
import com.xycake.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/cake")
    public Map<String,Object> xycakeHome(Integer id){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", goodsService.selectGoodsById(id).get(0));
        map.put("img",goodsService.selectImgById(id).get(0));
        return map;
    }

    @RequestMapping("/goodsselect")
    public Map<String,Object> xycakegoods(Integer id){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", goodsService.selectGoodsById(id));
        return map;
    }

    @RequestMapping("/order")
    public void xycakeOrder(Integer id, String ctime, HttpSession session) {
        Order order=new Order();
        order.setUid(id);//商品id
        order.setCtime(ctime);//商品订单时间
        order.setPrice((goodsService.selectPriceById(id)).getPrice());
        User u = (User) session.getAttribute("loginUser");
        order.setState(u.getId());//用户id
        order.setPtime("未完成");//商品状态
        order.setDelMark(0);//是否删除
        orderService.insertOrder(order);
    }
}
